package com.px.user.controller;

import com.px.common.core.Response;
import com.px.user.entity.AdminOps;
import com.px.user.mapper.AdminOpsMapper;
import com.px.user.model.PagingResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import java.sql.Timestamp;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/adminOps")
@Api(value = "操作记录", tags = {"操作记录管理"})
public class AdminOpsController {

    @Autowired
    private AdminOpsMapper adminOpsMapper;

    @GetMapping("/get")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation(value = "操作记录", authorizations = {@Authorization(value = "token")}, tags = {"操作"})
    public Response<PagingResp<AdminOps>> get(
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "用户ID", example = "1") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam(value = "分类") @RequestParam(value = "category", required = false) String category,
            @ApiParam(value = "角色") @RequestParam(value = "role", required = false) String role,
            @ApiParam(value = "url") @RequestParam(value = "url", required = false) String url,
            @ApiParam(value = "值") @RequestParam(value = "value", required = false) String value,
            @ApiParam(value = "备注") @RequestParam(value = "remark", required = false) String remark,
            @ApiParam(value = "开始时间", example = "1566805792293") @RequestParam(value = "qstart", required = false) Long qstart,
            @ApiParam(value = "截止时间") @RequestParam(value = "qend", required = false) Long qend,
            @ApiParam(value = "排序字段(倒序)", example = "createTime") @RequestParam(value = "orderField", required = false, defaultValue = "createTime") String orderField,
            @ApiParam(value = "页码", example = "0") @RequestParam(value = "page", required = false, defaultValue = "0") @Min(0) Integer page,
            @ApiParam(value = "返回数量", example = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") @Min(1) Integer limit) {

        AdminOps qadminOps = AdminOps.builder()
                .id(id)
                .userId(userId)
                .category(category)
                .role(role)
                .url(url)
                .value(value)
                .remark(remark)
                .qstart(qstart == null ? null : new Timestamp(qstart))
                .qend(qend == null ? null : new Timestamp(qend))
                .orderField(orderField)
                .startIndex(page * limit)
                .limit(limit)
                .build();

        List<AdminOps> adminOpsList = adminOpsMapper.paging(qadminOps);
        return Response.success(PagingResp.<AdminOps>builder()
                .itemCount(adminOpsMapper.pagingCount(qadminOps))
                .items(adminOpsList).build());
    }
}
